﻿
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace NetCore
{
    public class EntityBase
    {

        /// <summary>
        /// 创建人
        /// </summary>
        public const string __CreateBy = "CreateBy";

        /// <summary>
        /// 创建时间
        /// </summary>
        public const string __CreateDate = "CreateDate";

        /// <summary>
        /// 修改人
        /// </summary>
        public const string __LastModifyUserId = "LastModifyUserId";

        /// <summary>
        /// 修改人
        /// </summary>
        public const string __LastModifyDate = "LastModifyDate";


        /// <summary>
        /// 逻辑删除标识
        /// </summary>
        public const string __IsDeleted = "IsDeleted";

        private string _createBy;
        private DateTime _createDate = DateTime.Now;
        private string _lastModifyUserId ;
        private DateTime _lastModifyDate = DateTime.Now;
        private bool _isDeleted = false;
        private bool _isUpdated = false;
        private bool _isDefaultUser = true;

        [DbColumn("Province", LocalName = "Province", AllowDBNull = true, IsSystem = true, IsHide = true, IsEnabled = false, Insert = false, Update = false)]
        public virtual string Province { get; set; }
        [DbColumn("City", LocalName = "City", AllowDBNull = true, IsSystem = true, IsHide = true, IsEnabled = false, Insert = false, Update = false)]
        public virtual string City { get; set; }

        [DbColumn("County", LocalName = "County", AllowDBNull = true, IsSystem = true, IsHide = true, IsEnabled = false, Insert = false, Update = false)]
        public virtual string County { get; set; }


        /// <summary>
        /// 是否是更新
        /// </summary>
        [DbColumn("IsUpdated", LocalName = "IsUpdated", AllowDBNull = false, IsSystem = true, IsHide = true, IsEnabled = false, Insert = false, Update = false)]
        public virtual bool IsUpdated { get { return _isUpdated; } set { _isUpdated = value; } }
        [DbColumn("CreateBy", LocalName = "创建人", AllowDBNull = false, IsSystem = true, IsHide = true, Update = false)]
        public virtual string CreateBy
        {
            get
            {
                if (!IsUpdated && IsDefaultUser)
                {
                    return _createBy;;
                   // return ApplicationEnvironments.DefaultSession.UserId.ToInt();
                }
                else
                {
                    return _createBy;
                }
            }
            set { SetUpdateField(__CreateBy); _createBy = value; }
        }
        [DbColumn("CreateDate", LocalName = "创建时间", AllowDBNull = false, IsSystem = true, IsHide = true, Format = "yyyy-MM-dd hh:mm:ss", Update = false)]
        [JsonProperty]
        public virtual DateTime CreateDate
        {
            get
            {
               return _createDate; 
            }
            set {
               // SetUpdateField(__CreateDate);
                _createDate = value; }
        }

        [DbColumn("LastModifyUserId", LocalName = "修改人", AllowDBNull = false, IsSystem = true, IsHide = true)]
        [JsonProperty]
        public virtual string LastModifyUserId
        {
            get
            {
                return _lastModifyUserId;
            }
            set { SetUpdateField(__LastModifyUserId); _lastModifyUserId = value; }
        }

        [DbColumn("LastModifyDate", LocalName = "修改时间", AllowDBNull = false, ColumnType = "date", GridWidth = 150, IsSystem = true, Format = "yyyy-MM-dd hh:mm:ss")]
        [JsonProperty]
        public virtual DateTime LastModifyDate
        {
            get
            {
                return _lastModifyDate;
            }
            set { SetUpdateField(__LastModifyDate); _lastModifyDate = value; }
        }
        [DbColumn("IsDeleted", LocalName = "是否删除", AllowDBNull = false, Default = "false", IsSystem = true, IsHide = true)]
        [JsonProperty]
        public virtual bool IsDeleted { get { return _isDeleted; } set { _isDeleted = value; } }

        /// <summary>
        /// 启用系统默认用户
        /// </summary>
        public virtual bool IsDefaultUser { get { return _isDefaultUser; } set { _isDefaultUser = value; } }
       
        [JsonIgnore]
        /// <summary>
        /// 更新字段
        /// </summary>
        public virtual List<string> UpdateField { get; set; }

        [JsonIgnore]
        /// <summary>
        /// 表名
        /// </summary>
        public virtual string TableName { get; set; }
        [JsonIgnore]
        /// <summary>
        /// 主键
        /// </summary>
        public virtual string PrimaryKeyName { get; set; }

        public EntityBase()
        {
            UpdateField = new List<string>();
        }
      
        public virtual void SetUpdateField(string field)
        {
            if (!UpdateField.Contains(field))
            {
                UpdateField.Add(field);
            }

        }

    }
}
